﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Web;
using Microsoft.SharePoint;

namespace CKS.EBE.WebControls
{
    public class UserDetails : XmlBasedControl
    {
        protected override System.Xml.XmlDocument BuildDocument()
        {
            try
            {
                XmlDocument oDoc = new XmlDocument();
                oDoc.LoadXml("<UserDetails/>");

                if (HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    System.Security.Principal.IPrincipal oPrincipal = HttpContext.Current.User;
                    oDoc.DocumentElement.SetAttribute("IsAuthenticated", "True");
                    oDoc.DocumentElement.SetAttribute("UserName", oPrincipal.Identity.Name);

                    if (SPContext.Current.Web != null && SPContext.Current.Web.CurrentUser != null)
                    {
                        SPUser oCurrent = SPContext.Current.Web.CurrentUser;
                        oDoc.DocumentElement.SetAttribute("Email", oCurrent.Email);
                        oDoc.DocumentElement.SetAttribute("IsSiteAdmin", oCurrent.IsSiteAdmin.ToString());
                        oDoc.DocumentElement.SetAttribute("LoginName", oCurrent.LoginName);
                        oDoc.DocumentElement.SetAttribute("Name", oCurrent.Name);
                        oDoc.DocumentElement.SetAttribute("Notes", oCurrent.Notes);

                        XmlDocument oUserXML = new XmlDocument();
                        oUserXML.LoadXml(oCurrent.Xml);
                        oDoc.DocumentElement.AppendChild(oDoc.ImportNode(oUserXML.DocumentElement, true));
                    }
                }
                else
                {
                    oDoc.DocumentElement.SetAttribute("IsAuthenticated", "False");
                }

                return oDoc;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex);
                return null;
            }
        }
    }
}
